package com.fh.wanandroid.ui.project.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.fh.wanandroid.R;
import com.fh.wanandroid.app.Constants;
import com.fh.wanandroid.base.BaseFragment;
import com.fh.wanandroid.ui.knowledge.fragment.KnowledgeListFragment;
import com.fh.wanandroid.ui.project.bean.ProjectData;
import com.fh.wanandroid.ui.project.contract.ProjectContract;
import com.fh.wanandroid.ui.project.presenter.ProjectPresenter;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ProjectFragment extends BaseFragment<ProjectContract.Presenter> implements ProjectContract.View {


    List<ProjectListFragment> mFragments;


    List<ProjectData.DataBean> projectList;
    @BindView(R.id.project_tab_layout)
    SlidingTabLayout projectTabLayout;
    @BindView(R.id.project_viewpager)
    ViewPager projectViewpager;


    @Override
    public ProjectContract.Presenter getmPresenter() {
        return new ProjectPresenter();
    }


    public static ProjectFragment getInstance(String title, int cid) {

        ProjectFragment fragment = new ProjectFragment();
        Bundle args = new Bundle();
        args.putString(Constants.ARG_PARAM1, title);
        args.putInt(Constants.ARG_PARAM2, cid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void presenterSetView() {

        if (mPresenter != null) {
            mPresenter.setView(this);
        }

    }

    @Override
    public void initView() {
        mFragments = new ArrayList<>();
        projectList = new ArrayList<>();


    }

    @Override
    public void initData() {


        mPresenter.getProjectData();


    }

    @Override
    public void loadData() {


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_project;
    }

    @Override
    public void getProjectClass(ProjectData projectData) {


        projectList.addAll(projectData.getData());

        if (projectList == null) {
            return;
        }

        for (ProjectData.DataBean data : projectList) {
            mFragments.add(ProjectListFragment.getInstance(data.getName(), data.getId()));
        }
        Log.e("ProjectFragment ", "-------mFragments:" + mFragments.size());
        ProjectVpAdaper projectVpAdaper = new ProjectVpAdaper(getFragmentManager(), getContext(), mFragments);
        projectViewpager.setAdapter(projectVpAdaper);
        projectTabLayout.setViewPager(projectViewpager);

    }

    @Override
    public void loadDataFail(String msg) {

    }

    public class ProjectVpAdaper extends FragmentPagerAdapter {

        List<ProjectListFragment> fragmentList;

        Context context;

        public ProjectVpAdaper(FragmentManager fm, Context context, List<ProjectListFragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;

        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return projectList.get(position).getName();
        }
    }

}
